<?php

namespace App\Models\Shop;

use App\Models\User\WxUser;
use Carbon\Carbon;
use Dcat\Admin\Traits\HasDateTimeFormatter;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class WxShopOrderGoods extends Model
{
	use HasDateTimeFormatter;
    use SoftDeletes;

    protected $table = 'wx_shop_order_goods';

    protected $appends = ['to_target_type'];

    public function buyer()
    {
        return $this->hasOne(WxUser::class, 'id', 'buyer_user_id');
    }

    public function seller()
    {
        return $this->hasOne(WxUser::class, 'id', 'seller_user_id');
    }

    public function order()
    {
        return $this->hasOne(WxShopOrder::class, 'id', 'order_id');
    }

    public function addAll(Array $data)
    {
        $rs = DB::table($this->getTable())->insert($data);
        return $rs;
    }

    public function getDownloadUrlAttribute($value)
    {
        if(_empty_($value)){
            return '';
        }
        if($this->state == 0) {
            return $value;
        }
        return '**********';
    }

    public function getPaidContentAttribute($value)
    {
        if(_empty_($value)){
            return '';
        }
        if($this->state == 0){
            return $value;
        }
        return '**********';
    }

    public function getRefundDeadlineAttribute($value)
    {
        // 0: 不可退款 1：可退款 2：已超时
        if(_empty_($value)){
            return 1;
        }
        if($value == $this->created_at){
            return 0;
        }
        if($value >= Carbon::now()){
            return 1;
        }
        return 2;
    }

    public function getToTargetTypeAttribute($value)
    {
        if($this->goods_type == 6){
            return 28;
        }else if($this->goods_type == 7){
            return 34;
        }else{
            return 3;
        }
    }

}
